package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.CacheSupportUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import com.ruoyi.factorydata.mapper.WarehouseInfoMapper;
import com.ruoyi.factorydata.domain.WarehouseInfo;
import com.ruoyi.factorydata.service.IWarehouseInfoService;

/**
 * 仓库信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-16
 */
@Service
public class WarehouseInfoServiceImpl extends ServiceImpl<WarehouseInfoMapper, WarehouseInfo> implements IWarehouseInfoService {

    private final WarehouseInfoMapper warehouseInfoMapper;
    private final ISysUserService sysUserService;
    public WarehouseInfoServiceImpl(WarehouseInfoMapper warehouseInfoMapper, ISysUserService sysUserService) {
        this.warehouseInfoMapper = warehouseInfoMapper;
        this.sysUserService = sysUserService;
    }


    /**
     * 查询仓库信息
     *
     * @param id 仓库信息主键
     * @return 仓库信息
     */
    @Override
    public WarehouseInfo selectWarehouseInfoById(Long id) {
        return getById(id);
    }

    /**
     * 查询仓库信息列表
     *
     * @param warehouseInfo 仓库信息
     * @return 仓库信息
     */
    @Override
    public List<WarehouseInfo> selectWarehouseInfoList(WarehouseInfo warehouseInfo) {
        List<WarehouseInfo> warehouseInfos = warehouseInfoMapper.selectWarehouseInfoList(warehouseInfo);
        HashMap<Long, String> map = new HashMap<>();
        warehouseInfos.forEach(item -> {
            Long userId = Long.valueOf(item.getCreateBy());
            String username = map.get(userId);
            if(StringUtils.isBlank( username)){
                SysUser sysUser = sysUserService.selectUserById(userId);
                if(sysUser!=null){
                    username=sysUser.getNickName();
                    map.put(userId, username);
                }
            }
            item.setCreateByName(username);
        });
        return warehouseInfos;
    }

    /**
     * 新增仓库信息
     *
     * @param warehouseInfo 仓库信息
     * @return 结果
     */
    @Override
    public int insertWarehouseInfo(WarehouseInfo warehouseInfo) {
        warehouseInfo.setCreateTime(DateUtils.getNowDate());
        warehouseInfo.setCreateBy(SecurityUtils.getLoginUser().getUserId() + "");
        return baseMapper.insertWarehouseInfo(warehouseInfo);
    }

    /**
     * 修改仓库信息
     *
     * @param warehouseInfo 仓库信息
     * @return 结果
     */
    @Override
    public int updateWarehouseInfo(WarehouseInfo warehouseInfo) {
        warehouseInfo.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateWarehouseInfo(warehouseInfo);
    }

    /**
     * 批量删除仓库信息
     *
     * @param ids 需要删除的仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseInfoByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除仓库信息信息
     *
     * @param id 仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseInfoById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public String getNameByIdAndCache(Long warehouseId, HashMap<Long, String> warehouseCache) {
        return CacheSupportUtils.getFromCacheOrDB(warehouseId,warehouseCache,this::selectWarehouseInfoById,WarehouseInfo::getWarehouseName,false);
    }
}